﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DX.CRC.Common.Entities;
using StuMngVan.Entities;

namespace StuMngVan.Dals
{
    class Dal4Teacher : DalBase
    {
        public static List<Teacher> SearchAllTeachers(string txtname)
        {
            QueryObject qo = CreateQO();

            qo.CmdText = @" SELECT Tch.[ID]
                            ,Tch.[Name]
                            ,Tch.[TeachingExperience]
                            ,Cs.[Name] as Course
                            ,Tch.[Remark]
                          FROM [dbo].[TbTeacher] Tch LEFT JOIN [dbo].[TbCourse] Cs 
                          ON Tch.CourseID= Cs.ID
                          WHERE Tch.IsActive=1 and Tch.[Name] like '%'+@txtName+'%' ";

            qo.AddParam("@txtName",txtname);

            return qo.xExecuteList<Teacher>();
        }

        public static void InsertTeacher(string name, int experience, int courseID,string remark)
        {
            QueryObject qo = CreateQO();

            qo.CmdText = @"INSERT INTO [dbo].[TbTeacher]
                   ([Name]
                    ,[TeachingExperience]
                    ,[CourseID]
                    ,[IsActive]
                    ,[CreateTime]
                    ,[Remark])
             VALUES
                   (@Name
                   ,@TeachingExperience
                   ,@CourseID
                   ,1
                   ,getutcdate()
                   ,@Remark)";

            qo.AddParam("@Name", name);
            qo.AddParam("@TeachingExperience", experience);
            qo.AddParam("@courseID", courseID);
            qo.AddParam("@Remark", remark);

            qo.xExecuteNonQuery();
        }

        public static void DeleteTeacher(Teacher teacher)
        {
            QueryObject qo = CreateQO();

            qo.CmdText = @"update TbTeacher 
                set IsActive = 0 
                WHERE ID=@TeacherID";

            qo.AddParam("@TeacherID", teacher.ID);

            qo.xExecuteNonQuery();
        }
        public static DataTable GetCourses()
        {
            QueryObject qo = CreateQO();
            qo.CmdText = @"select ID as CourseID
	            , Name as CourseName
            from TbCourse";

            return qo.xExecuteDataTable();
        }
    }
}
